package com.itheima.executorservice1;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class Test3 {

    public static void main(String[] args) {

        //方式三：通过线程池工具类Executors，调用其静态方法直接得到线程池对象
        //这些方法的底层都是通过线程池的实现类ThreadPoolExecutor来创建线程对象

        //注意：大型并发系统环境中使用Executors容易出现系统风险

        ExecutorService pool = Executors.newFixedThreadPool(3);     //创建固定大小的线程池

        Future<String> f1 = pool.submit(new MyCallable(100));
        Future<String> f2 = pool.submit(new MyCallable(200));
        Future<String> f3 = pool.submit(new MyCallable(300));
        Future<String> f4 = pool.submit(new MyCallable(400));
        try {
            System.out.println(f1.get());
            System.out.println(f2.get());
            System.out.println(f3.get());
            System.out.println(f4.get());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //多线程是并发和并行同时存在的
    //并发：CPU分时轮询地执行
    //并行：同一时刻同时在执行
}
